# -*- coding: utf-8 -*-

# src\widgets\GraphWidget.py

# 引入pyqt5.QtWidgets的模块
from PyQt5.QtWidgets import (
    QVBoxLayout, 
    QWidget,
)

# 引入matplotlib.backends.backend_qt5agg的FigureCanvasQTAgg类并起名为FigureCanvas
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
# 引入matplotlib.figure.Figure类并起名为Figure
from matplotlib.figure import Figure
# 引入matplotlib.pyplot并起名为plt
import matplotlib.pyplot as plt
# 引入matplotlib.cm模块
from matplotlib import cm

class QEGraphWidget(QWidget):
    def __init__(self,row,col):
        super().__init__()
        
        # 创建一个Figure实例
        self.figure = Figure()
        # 调整子图间距
        self.figure.subplots_adjust(left=0.02, bottom=0.02, right=0.98, top=0.95)
        # FigureCanvasQTAgg将Figure渲染到Qt界面上
        self.canvas = FigureCanvas(self.figure)
        
        # 布局管理器
        layout = QVBoxLayout()
        layout.setSpacing(0)

        # 将FigureCanvasQTAgg添加到布局管理器中
        layout.addWidget(self.canvas)
        #将布局管理器添加到窗口中
        self.setLayout(layout)

        # 初始化子图行列大小
        self.row = row
        self.col = col

        
    def plot_elevation_slope(self,dem_slope,cmap_name,index,title="Elevation Slope"):
        
        # 设置颜色映射的透明度，使得无效值不显示
        # lut=None 可以保留原始颜色映射的长度
        cmap = cm.get_cmap(cmap_name, lut=None)
        # alpha=0 使得无效值透明
        cmap.set_bad(alpha=0)  

        # 使用add_subplot方法添加子图
        self.axes = self.figure.add_subplot(self.row, self.col, index)  

        # 使用add_subplot后得到的axes对象来绘图
        self.axes.imshow(dem_slope, cmap=cmap)
        # 设置图幅标题
        self.axes.set_title(title)
        # 去除X轴刻度
        self.axes.set_xticks([])
        # 去除Y轴刻度
        self.axes.set_yticks([])
        # 更新画布
        self.canvas.draw_idle()  


